package com.jiazhong.boot.rocketmq.controller;

import com.jiazhong.boot.commons.util.JsonResult;
import com.jiazhong.boot.commons.util.ResultTool;
import jakarta.annotation.Resource;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.TransactionSendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/rocket_mq")
public class RocketMQController {

    private RocketMQTemplate rocketMQTemplate;

    private RocketMQTemplate rocketMQTransactionTemplate;
    @Autowired
    public RocketMQController(RocketMQTemplate rocketMQTemplate, RocketMQTemplate rocketMQTransactionTemplate) {
        this.rocketMQTemplate = rocketMQTemplate;
        this.rocketMQTransactionTemplate = rocketMQTransactionTemplate;

        System.out.println(rocketMQTemplate.getProducer());
        System.out.println(rocketMQTransactionTemplate.getProducer());
    }


    @GetMapping("/a")
    public JsonResult a() {
        String message = "hello,sync!";
        SendResult sendResult = rocketMQTemplate.syncSend("TopicB:sync", message);

        return ResultTool.success(sendResult);
    }

    @GetMapping("/b")
    public JsonResult b(int number, String body) {
        Message<String> message = MessageBuilder.withPayload(body + "1")
                .setHeader("KEYS", number + "")
                .build();
        TransactionSendResult sendResult = rocketMQTransactionTemplate
                .sendMessageInTransaction("tx01","TopicC:transaction", message, body + "2");
        //rocketMQTemplate.sendMessageInTransaction()
        return ResultTool.success(sendResult);
    }

}
